package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HCarReserveMapper;
import com.flea.hclv.model.dao.HCarReservePayinfoMapper;
import com.flea.hclv.model.entity.HCarReserve;
import com.flea.hclv.model.entity.HCarReservePayinfo;
import com.flea.hclv.service.HCarReservePayinfoService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: project
 * @description: 车队支付
 * @author: shiliqiang
 * @create: 2019-03-03 09:43
 * @version: 1.0
 */
@Service
public class HCarReservePayinfoServiceImpl implements HCarReservePayinfoService {

    @Autowired
    private HCarReservePayinfoMapper hCarReservePayinfoMapper;
    @Autowired
    private HCarReserveMapper hCarReserveMapper;

    @Override
    public ServerResponse<Map<String, String>> getPayMoney(String carReserveId) {
        List<HCarReservePayinfo> payinfos = hCarReservePayinfoMapper.selectByCarReverseId(carReserveId);
        Float pays = 0.f;
        Map<String,String> map = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(payinfos)){
            for (HCarReservePayinfo p :payinfos){
                pays += p.getPayMoney();
            }
            map.put("pays",pays.toString());
        }else {
            map.put("pays","0");
        }
        return ServerResponse.createBySuccess(map);
    }

    @Override
    public ServerResponse<String> addPayInfo(HttpServletRequest request, String model) throws Exception {
        String userId = FleaOrgUtils.getCurUserUuid(request);
        JSONObject jsonObject = JSON.parseObject(model);
        String carReverseId = jsonObject.getString("id");
        HCarReserve reserve = hCarReserveMapper.selectByPrimaryKey(carReverseId);
        if (reserve==null){
            return ServerResponse.createByErrorMessage("订单未找到");
        }
        List<HCarReservePayinfo> hCarReservePayinfos = hCarReservePayinfoMapper.selectByCarReverseId(carReverseId);
        Float shouldPay = reserve.getTotalCost();
        Float oldTotal = 0.f;
        if (CollectionUtils.isNotEmpty(hCarReservePayinfos)){
            for (HCarReservePayinfo payinfo : hCarReservePayinfos){
                oldTotal += payinfo.getPayMoney();
            }
            if (oldTotal!=null){
                int result = oldTotal.compareTo(shouldPay);
                if (result==0 || result>0){
                    return ServerResponse.createByErrorMessage("请不要重复支付");
                }
            }

        }

        List<HCarReservePayinfo> pays = JSONObject.parseArray(jsonObject.getString("pays"),HCarReservePayinfo.class);
        List<HCarReservePayinfo> newPays = Lists.newArrayList();
        Float newTotal=0f;
        for (HCarReservePayinfo payinfo : pays){
            payinfo.setId(FleaStringUtils.getGenerateUUID());
            payinfo.setCarReserveId(carReverseId);
            payinfo.setCreateUser(userId);;
            newPays.add(payinfo);
            newTotal += payinfo.getPayMoney();
        }

        int row = hCarReservePayinfoMapper.insertBatch(newPays);
        if (row<0){
            return ServerResponse.createByErrorMessage("操作失败");

        }
        newTotal += oldTotal;
        int result = newTotal.compareTo(shouldPay);
        if (result==0 || result>0){
            //更新订单状态
            reserve.setPayStatus(HConstant.D_CAR_PAY_STATUS_YES);
            reserve.setReserveStatus(HConstant.D_CAR_RESERVE_STATUS_END);
            row = hCarReserveMapper.updateByPrimaryKeySelective(reserve);
            if (row<0){
                throw new Exception("更新车队订单状态失败");

            }
        }else {
            //更新订单状态  部分支付
            reserve.setPayStatus(HConstant.D_CAR_PAY_STATUS_PART);
            row = hCarReserveMapper.updateByPrimaryKeySelective(reserve);
            if (row<0){
                throw new Exception("更新车队订单状态失败");

            }
        }

        return ServerResponse.createBySuccessMessage("操作成功");
    }
}
